package com.code.leetcode.idea;

public class LeetCodeIdea014 {
    public static void main(String[] args) {
        String[] ags = new String[]{"flower","flow","c"};
        LeetCodeIdea014 leetCodeIdea014 = new LeetCodeIdea014();
        String prefix = leetCodeIdea014.longestCommonPrefix(ags);
        System.out.println(prefix);

    }
    public String longestCommonPrefix(String[] strs) {
        String prefix = strs[0];
        for (int i = 1; i < strs.length; i++) {
            prefix = longestCommonPrefix(prefix, strs[i]);
            if (prefix.length() == 0) {
                return "";
            }
        }
        return prefix;
    }

    public String longestCommonPrefix(String str1, String str2) {
        int length = Math.min(str1.length(), str2.length());
        int index = 0;
        while (index < length && str1.charAt(index) == str2.charAt(index)) {
            index ++;
        }
        if (index > 0) {
            return str1.substring(0, index);
        }
        return "";
    }
}
